using System;
using System.Collections.Generic;
using Rhino.Commons;
using WineCellar.Core.Interfaces;
using WineCellar.Core.PropertyParsers;

namespace WineCellar.Core.PropertyParsers
{
    public class PropertyHelper
    {              
        private static List<IPropertyParser> propertyParsers;

        private static List<IPropertyParser> PropertyParsers
        {
            get
            {
                if (propertyParsers == null)
                {
                    propertyParsers = new List<IPropertyParser>();

                    propertyParsers.AddRange(IoC.ResolveAll<IPropertyParser>());
                }

                return propertyParsers;
            }
        }

        protected static IPropertyParser GetParser(Type type)
        {
            foreach (IPropertyParser parser in PropertyParsers)
            {
                if (parser.IsParsable(type) && parser.GetType() != typeof(DefaultPropertyParser))
                {
                    return parser;
                }

            }
            return new DefaultPropertyParser();
        }

        public static bool IsEmpty(object target)
        {
            if (target == null)
                return true;

            IPropertyParser parser = GetParser(target.GetType());

            return parser.IsEmpty(target);
        }
    }
}